# SPDX-License-Identifier: GPL-2.0-or-later

# not a GNU package. You can remove this line, if
# have all needed files, that a GNU package needs
AUTOMAKE_OPTIONS = gnu 1.6

.DELETE_ON_ERROR:

# make sure we pass the correct jimtcl flags to distcheck
AM_DISTCHECK_CONFIGURE_FLAGS = --disable-install-jim

# do not run Jim Tcl tests (esp. during distcheck)
check-recursive: SUBDIRS := $(SUBDIRS:jimtcl=)

nobase_dist_pkgdata_DATA = \
	contrib/libdcc/dcc_stdio.c \
	contrib/libdcc/dcc_stdio.h \
	contrib/libdcc/example.c \
	contrib/libdcc/README \
	contrib/60-openocd.rules

SUBDIRS =
DIST_SUBDIRS =
bin_PROGRAMS =
noinst_LTLIBRARIES =
info_TEXINFOS =
dist_man_MANS =
EXTRA_DIST =
DISTCLEANFILES =

if INTERNAL_JIMTCL
SUBDIRS += jimtcl
DIST_SUBDIRS += jimtcl
EXTRA_DIST += jimtcl/configure.gnu
# jimtcl from 0.79 to 0.82 miss cleaning jsmn.o
DISTCLEANFILES += jimtcl/jsmn/jsmn.o
endif

SUBDIRS += testing
DIST_SUBDIRS += testing

# common flags used in openocd build
AM_CFLAGS = $(GCC_WARNINGS)
AM_LDFLAGS =

AM_CPPFLAGS = $(HOST_CPPFLAGS)\
			  -I$(top_srcdir)/src \
			  -I$(top_builddir)/src \
			  -DPKGDATADIR=\"$(pkgdatadir)\" \
			  -DBINDIR=\"$(bindir)\"

if INTERNAL_JIMTCL
AM_CPPFLAGS += -I$(top_srcdir)/jimtcl \
			   -I$(top_builddir)/jimtcl
else
AM_CPPFLAGS += $(JIMTCL_CFLAGS)
endif

if USE_GCOV
AM_CFLAGS += --coverage
AM_LDFLAGS += --coverage
endif

EXTRA_DIST += \
	BUGS \
	HACKING \
	NEWTAPS \
	README.Windows \
	README.macOS \
	$(EXTRA_DIST_NEWS) \
	Doxyfile.in \
	LICENSES/license-rules.txt \
	LICENSES/dual/CC-BY-4.0 \
	LICENSES/exceptions/eCos-exception-2.0 \
	LICENSES/preferred/BSD-1-Clause \
	LICENSES/preferred/BSD-2-Clause \
	LICENSES/preferred/BSD-2-Clause-Views \
	LICENSES/preferred/BSD-3-Clause \
	LICENSES/preferred/BSD-Source-Code \
	LICENSES/preferred/CC0-1.0 \
	LICENSES/preferred/GFDL-1.2 \
	LICENSES/preferred/gfdl-1.2.texi.readme \
	LICENSES/preferred/GPL-2.0 \
	LICENSES/preferred/LGPL-2.1 \
	LICENSES/preferred/MIT \
	LICENSES/stand-alone/Apache-2.0 \
	LICENSES/stand-alone/GPL-3.0 \
	tools/logger.pl \
	tools/rlink_make_speed_table \
	tools/st7_dtc_as \
	contrib

libtool: $(LIBTOOL_DEPS)
	$(SHELL) ./config.status --recheck

docs: pdf html doxygen

Doxyfile: $(srcdir)/Doxyfile.in
	@echo "Creating $@ from $<..."
	@( \
	  echo "### @@@ -= DO NOT EDIT THIS FILE =- @@@ ###" && \
	  echo "### @@@ Make changes to Doxyfile.in @@@ ###" && \
	  sed -e 's,@srcdir\@,$(srcdir),' \
	    -e 's,@builddir\@,$(builddir),' \
	    -e 's,@doxygen_as_html\@,$(doxygen_as_html),' \
	    -e 's,@doxygen_as_pdf\@,$(doxygen_as_pdf),' $< \
	) > $@

THE_MANUAL = doxygen/latex/refman.pdf

doxygen::
	$(MAKE) Doxyfile
	doxygen Doxyfile 2>&1 | perl $(srcdir)/tools/logger.pl > doxygen.log
	@if [ -f doxygen/latex/refman.tex ]; then \
		echo "Creating $(THE_MANUAL)..."; \
		$(MAKE) $(THE_MANUAL); \
	else \
		echo "Skipping Doxygen PDF..."; \
	fi

$(THE_MANUAL): %.pdf: %.tex
	-cd $$(dirname $*) && pdflatex $$(basename $*)
	-cd $$(dirname $*) && pdflatex $$(basename $*)

TCL_PATH = tcl
# command to find paths of script files, relative to TCL_PATH
TCL_FILES = find $(srcdir)/$(TCL_PATH) -name '*.cfg' -o -name '*.tcl' -o -name '*.txt' | \
		sed -e 's,^$(srcdir)/$(TCL_PATH),,'

# The git log command below generates many empty text lines with only some space characters
# for indentation purposes, so use sed to trim all trailing whitespace.
dist-hook:
	if test -d $(srcdir)/.git -a \( ! -e $(distdir)/ChangeLog -o -w $(distdir)/ChangeLog \) ; then \
		git --git-dir $(srcdir)/.git log --date=short --pretty="format:%ad  %aN <%aE>%n%n%w(0,4,6)* %B" \
		| sed 's/[[:space:]]*$$//' > $(distdir)/ChangeLog.tmp && \
		mv $(distdir)/ChangeLog.tmp $(distdir)/ChangeLog; \
	fi
	for i in $$($(TCL_FILES)); do \
		j="$(distdir)/$(TCL_PATH)/$$i" && \
		mkdir -p "$$(dirname $$j)" && \
		$(INSTALL_DATA) $(srcdir)/$(TCL_PATH)/$$i $$j; \
	done

install-data-hook:
	for i in $$($(TCL_FILES)); do \
		j="$(DESTDIR)$(pkgdatadir)/scripts/$$i" && \
		mkdir -p "$$(dirname $$j)" && \
		$(INSTALL_DATA) $(srcdir)/$(TCL_PATH)/$$i $$j; \
	done

uninstall-hook:
	rm -rf $(DESTDIR)$(pkgdatadir)/scripts

distclean-local:
	rm -rf Doxyfile doxygen
	-rm -f $(srcdir)/jimtcl/configure.gnu

DISTCLEANFILES += doxygen.log

METASOURCES = AUTO

BUILT_SOURCES =
CLEANFILES =

MAINTAINERCLEANFILES = \
	%D%/INSTALL \
	%D%/configure \
	%D%/Makefile.in \
	%D%/depcomp \
	%D%/config.guess \
	%D%/config.sub \
	%D%/config.h.in \
	%D%/config.h.in~ \
	%D%/compile \
	%D%/ltmain.sh \
	%D%/missing \
	%D%/aclocal.m4 \
	%D%/install-sh \
	%D%/texinfo.tex

include src/Makefile.am
include doc/Makefile.am
